package com.ztao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.admin.dto.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.wemedia.pojos.WmChannel;
import com.ztao.mapper.WmChannelMapper;
import com.ztao.service.WmChannelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class WmChannelServiceImpl implements WmChannelService {
    @Resource
    private WmChannelMapper wmChannelMapper;
    @Override
    public ResponseResult findAll() {
        List<WmChannel> wmChannels = this.wmChannelMapper.selectList(null);
        return ResponseResult.okResult(wmChannels);
    }

    //频道列表
    @Override
    public ResponseResult findAll_channel(ChannelDto dto) {
        Page<WmChannel> page = new Page<>(dto.getPage(),dto.getSize());
        QueryWrapper<WmChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",dto.getName());
        queryWrapper.orderByDesc("created_time");
        page=this.wmChannelMapper.selectPage(page,queryWrapper);
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize()
                ,(int)page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public ResponseResult add(WmChannel wmChannel) {
        wmChannel.setIsDefault(true);
        wmChannel.setCreatedTime(new Date());
        this.wmChannelMapper.insert(wmChannel);
        return ResponseResult.okResult("添加success");
    }

    @Override
    public ResponseResult update(WmChannel wmChannel) {
        this.wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult("修改+success");
    }

    @Override
    public ResponseResult delete(Integer id) {
        this.wmChannelMapper.deleteById(id);
        return ResponseResult.okResult("删除+success");
    }
}
